// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
   address: {},
   cart: [],
   allChecked: false,
   totalPrice: 0,
   totalNum: 0
  },
  //获取缓存中的收货地址信息
  onShow() {
    const address = wx.getStorageSync('address');
    //给data复制
    const cart = wx.getStorageSync('cart') || [];
    //计算全选
    //every是一个数组方法，会接受一个回调
    //只有回调都是true
    //every才会是true
    // const allChecked = cart.length? cart.every(v => {return v.checked;}) : false;
    this.setCartData(cart); //<==并入该方法
    this.setData({
      address, //地址
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //获取地址的事件
  onaddress() {
    // 获取用户的收货地址
    /* 
    1 因为选择取消权限时则无法重新获取权限
    2 所以要诱导用户给予权限
    */
   //官方已优化
  wx.chooseAddress({
    success: (result) => {
      //将信息存入缓存
      wx.setStorageSync('address', result);
    }
   });
  },
  //点击选中事件
  onChecked(e) {
    //获取商品id
    const goods_id = e.currentTarget.dataset.id;
   //获取购物车数组
   let {cart} = this.data;
   //找到商品对象
   let index = cart.findIndex(v => v.goods_id === goods_id);
    cart[index].checked = !cart[index].checked;
    //把数据重新设置回data中
    this.setData({
      cart
    });
    //重新写入缓存
    this.setCartData(cart);
  },
  //购物车数据
  setCartData(cart) {
    let totalPrice = 0,
        totalNum = 0,
        allChecked = true; //重新全选会true时，重新设置回true
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    });
     //数组为空时
     allChecked = cart.length ? allChecked : false;
    this.setData({
      cart,  //购物车数组
      allChecked,
      totalPrice,
      totalNum
    });
    //重新写入缓存
    wx.setStorageSync('cart', cart);
  },
  //全选反选
  onAllChecked() {
    let allChecked = !this.data.allChecked;
    let cart = this.data.cart;
    cart.forEach(v => v.checked = allChecked);
    this.setData({
      allChecked,
      cart
    });
    this.setCartData(cart);
  },
  //数量编辑功能
  onNumEdit(e) {
    let {id, operation} = e.currentTarget.dataset;
    // console.log(id ,operation);
    let index = this.data.cart.findIndex(v => v.goods_id === id);
   let {cart} = this.data;
   //+-
   if (operation === -1 && cart[index].num === 1) {
      //删除功能
      this.onDelete("将删除该商品", () => {
        cart.splice(index, 1);
        this.setCartData(cart);
      });
   } else {
      operation === 1? cart[index].num += 1 : cart[index].num -= 1;
   }
  //  if (operation === -1 && cart[index].num > 0) {
  //   cart[index].num -= 1
  //   } else if (operation === 1) {
  //     cart[index].num += 1
  //   } else return;
   this.setData({
     cart
   });
   this.setCartData(cart);
  },
  //删除函数
  onDelete(content, Callback){
    wx.showModal({
      title: "提示",
      content,
      success: (res) => {
        if (res.confirm) {
          Callback();
        }
      } 
    })
  },
  //结算判断
  onPay() {
    //判断收货地址
    const {address, cart} = this.data;
    if (cart.length === 0) {
      this.myShowToast('您还没有选购商品，快去购物吧~');
      return ;
    }
    if (!address.userName) {
      this.myShowToast('您还没有选择收货地址~');
      return ;
    }
    wx.navigateTo({
      url: '../pay/pay',
    })
  },
  myShowToast(title) {
    wx.showToast({
      title,
      icon: 'none'
    })
  }
});